home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 01 / wintro4 / stock.c < prev    next >
C/C++ Source or Header  |  1990-12-31  |  23KB  |  760 lines

  1. /*
  2.   Microsoft Systems Journal Stock Application
  3.   Written by Marc Adler
  4.              Magma Systems
  5.              15 Bodwell Terrace
  6.              Millburn, New Jersey  07041
  7.  
  8.   This is for the fourth article. It is the basic skeleton of our
  9.   application. We add MDI capabilities to the code.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "windows.h"
  15. #include "stock.h"
  16.  
  17.  
  18. HANDLE hThisInstance;         /* Program instance handle */
  19. HWND hWndMain      = NULL;    /* Handle to main window   */
  20. HWND hwndMDIClient = NULL;    /* Handle to MDI client    */
  21. HWND hWndActive    = NULL;    /* Handle of the active MDI child */
  22. HWND hWndStatus    = NULL;    /* Window for status mesages */
  23.  
  24. HANDLE hCurrStockInfo;
  25. HANDLE hAccelTable;
  26.  
  27. WORD   iLastCommand;
  28.  
  29.  
  30. /****************************************************************************/
  31. /*                                                                          */
  32. /* Function : WinMain()                                                     */
  33. /*                                                                          */
  34. /* Purpose  : Entry point for our program. Calls the initialization         */
  35. /*            routines, creates the main window, and goes into the          */
  36. /*            standard windows message loop (with MDI calls thrown in).     */
  37. /*                                                                          */
  38. /* Returns  : 0                                                             */
  39. /*                                                                          */
  40. /****************************************************************************/
  41. int NEAR PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  42.   HANDLE hInstance;
  43.   HANDLE hPrevInstance;
  44.   LPSTR  lpszCmdLine;
  45.   int    nCmdShow;
  46. {
  47.   MSG msg;
  48.  
  49.   /*
  50.     Save the handle to this inctance.
  51.   */
  52.   hThisInstance = hInstance;
  53.  
  54.   /* 
  55.     If this is the first instance of the app, register window classes...
  56.   */
  57.   if (!hPrevInstance)
  58.   {
  59.     if (!InitializeApplication())
  60.     {
  61.       MessageBeep(0);
  62.       return FALSE;
  63.     }
  64.   }
  65.  
  66.   /* 
  67.     Perform the initialization for this particular instance...
  68.   */
  69.   if (!InitializeInstance(lpszCmdLine, nCmdShow))
  70.   {
  71.     return FALSE;
  72.   }
  73.  
  74.  
  75.   /*
  76.     The main message loop...
  77.   */
  78.   while (GetMessage(&msg, (HWND) NULL, 0, 0))
  79.   {
  80.     /* If a keyboard message is for the MDI , let the MDI client
  81.      * take care of it.  Otherwise, check to see if it's a normal
  82.      * accelerator key (like F3 = find next).  Otherwise, just handle
  83.      * the message as usual.
  84.      */
  85.     if (!TranslateMDISysAccel(hwndMDIClient, &msg) && 
  86.         !TranslateAccelerator(hWndMain, hAccelTable, &msg))
  87.     {
  88.       TranslateMessage(&msg);
  89.       DispatchMessage(&msg);
  90.     }
  91.   }
  92.  
  93.   return msg.wParam;
  94. }
  95.  
  96.  
  97. /****************************************************************************/
  98. /*                                                                          */
  99. /* Function : InitializeApplication()                                       */
  100. /*                                                                          */
  101. /* Purpose  : Takes care of the per-application initialization. This        */
  102. /*            means registering all of our classes.                         */
  103. /*                                                                          */
  104. /* Returns  : FALSE if a class could not be registered, else TRUE.          */
  105. /*                                                                          */
  106. /****************************************************************************/
  107. BOOL FAR PASCAL InitializeApplication(VOID)
  108. {
  109.   WNDCLASS wc;
  110.  
  111.   /*
  112.     Register the main window class
  113.   */
  114.   wc.style         = CS_VREDRAW | CS_HREDRAW;
  115.   wc.lpfnWndProc   = MainWndProc;
  116.   wc.cbClsExtra    = 0;
  117.   wc.cbWndExtra    = 0;
  118.   wc.hInstance     = hThisInstance;
  119.   wc.hIcon         = LoadIcon(hThisInstance, MAKEINTRESOURCE(ID_STOCK));
  120.   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  121.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  122.   wc.lpszMenuName  = "StockMenu";
  123.   wc.lpszClassName = "StockMainWindow";
  124.   if (!RegisterClass(&wc))
  125.     return FALSE;
  126.  
  127.   /* Register the MDI child class */
  128.   wc.lpfnWndProc   = GraphWndProc;
  129.   wc.hIcon         = LoadIcon(hThisInstance, MAKEINTRESOURCE(ID_GRAPH));
  130.   wc.lpszMenuName  = NULL;
  131.   wc.cbWndExtra    = CBWNDEXTRA;
  132.   wc.lpszClassName = "GraphWindow";
  133.   if (!RegisterClass(&wc))
  134.     return FALSE;
  135.  
  136.   wc.style         = CS_VREDRAW | CS_HREDRAW;
  137.   wc.lpfnWndProc   = StatusWndProc;
  138.   wc.cbClsExtra    = 0;
  139.   wc.cbWndExtra    = 0;
  140.   wc.hInstance     = hThisInstance;
  141.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  142.   wc.lpszMenuName  = NULL;
  143.   wc.lpszClassName = "StockStatus";
  144.   if (!RegisterClass(&wc))
  145.     return FALSE;
  146.  
  147.   return TRUE;
  148. }
  149.  
  150.  
  151. /****************************************************************************/
  152. /*                                                                          */
  153. /* Function : InitializeInstance()                                          */
  154. /*                                                                          */
  155. /* Purpose  : Performs per-instance initialization. This means creating     */
  156. /*            the main window (which in turn creates the MDI client) and    */
  157. /*            loading in the accelerator table.                             */
  158. /*                                                                          */
  159. /* Returns  : FALSE if it couldn't do the init, TRUE if successful.         */
  160. /*                                                                          */
  161. /****************************************************************************/
  162. BOOL FAR PASCAL InitializeInstance(LPSTR lpCmdLine, WORD nCmdShow)
  163. {
  164.   /* 
  165.     Create the frame
  166.   */
  167.   hWndMain = CreateWindow("StockMainWindow",
  168.                           "MSJ Stock Application",
  169.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  170.                           CW_USEDEFAULT, 0,
  171.                           CW_USEDEFAULT, 0,
  172.                           (HWND) NULL,
  173.                           (HMENU) NULL,
  174.                           hThisInstance,
  175.                           (LPSTR) NULL);
  176.   if (!hWndMain)
  177.     return FALSE;
  178.  
  179.   /* 
  180.     The MDI client should have been created in response to WM_CREATE
  181.     message for the frame window.
  182.   */
  183.   if (!hwndMDIClient)
  184.     return FALSE;
  185.  
  186.   /*
  187.     Load the accelerators in
  188.   */
  189.   hAccelTable = LoadAccelerators(hThisInstance, (LPSTR) "StockAccelerators");
  190.  
  191.   /* 
  192.     Display the main window
  193.   */
  194.   ShowWindow(hWndMain, nCmdShow);
  195.   UpdateWindow(hWndMain);
  196.  
  197.   return TRUE;
  198. }
  199.  
  200.  
  201. /****************************************************************************/
  202. /*                                                                          */
  203. /* Function : MainWndProc()                                                 */
  204. /*                                                                          */
  205. /* Purpose  : Window proc for the main window.                              */
  206. /*                                                                          */
  207. /****************************************************************************/
  208. LONG FAR PASCAL MainWndProc(hWnd, msg, wParam, lParam)
  209.   HWND hWnd;
  210.   WORD msg;
  211.   WORD wParam;
  212.   LONG lParam;
  213. {
  214.   int         rc;
  215.   HDC         hDC;
  216.   PAINTSTRUCT ps;
  217.   FARPROC     lpfn;
  218.   HANDLE      hStockInfo;
  219.   LPSTOCKINFO lpStockInfo;
  220.  
  221.   char        szMsg[80];
  222.   RECT        rClient;
  223.   int         i;
  224.  
  225.   static HWND ahwndSubMenus[16];
  226.  
  227.  
  228.   switch (msg)
  229.   {
  230.     case WM_CREATE:
  231.     {
  232.       CLIENTCREATESTRUCT ccs;
  233.  
  234.       /* Find window menu where children will be listed */
  235.       ccs.hWindowMenu  = GetSubMenu(GetMenu(hWnd), 3);
  236.       ccs.idFirstChild = ID_WINDOW_CHILDREN;
  237.  
  238.       /* Create the MDI client filling the client area */
  239.       hwndMDIClient = CreateWindow("mdiclient",
  240.                                     NULL,
  241.                                     WS_CHILD   | WS_CLIPCHILDREN,
  242.                                     0,0,0,0,
  243.                                     hWnd,
  244.                                     0xCAC,
  245.                                     hThisInstance,
  246.                                     (LPSTR) &ccs);
  247.       ShowWindow(hwndMDIClient, SW_SHOW);
  248.  
  249.       /*
  250.         Create a status window at the bottom of the main window.
  251.       */
  252.       GetClientRect(hWnd, (LPRECT) &rClient);
  253.       hWndStatus = CreateWindow("StockStatus",
  254.                                  NULL,
  255.                                  WS_CHILD | WS_BORDER,
  256.                                  rClient.left, rClient.bottom - Y_STATUSWINDOW,
  257.                                  rClient.right - rClient.left, Y_STATUSWINDOW,
  258.                                  hWnd,
  259.                                  NULL, 
  260.                                  hThisInstance,
  261.                                  (LPSTR) 0L);
  262.       if (hWndStatus)
  263.         ShowWindow(hWndStatus, SW_SHOW);
  264.  
  265.       /*
  266.         We gather up the window handles of all of the sub menus.
  267.       */
  268.       for (i = 0;  (ahwndSubMenus[i] = GetSubMenu(GetMenu(hWnd),i));  i++)
  269.         ;
  270.  
  271.       break;
  272.     }
  273.  
  274.  
  275.     case WM_COMMAND :
  276.       switch (iLastCommand = wParam)
  277.       {
  278.         case ID_NEW   :
  279.         case ID_GRAPH_OPTIONS :
  280.           if (wParam == ID_NEW || hCurrStockInfo == NULL)
  281.           {
  282.             if ((hStockInfo = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
  283.                                        (DWORD) sizeof(STOCKINFO))) == NULL)
  284.             {
  285.               MessageBox(hWnd, "No memory for stock info", NULL, MB_OK);
  286.               break;
  287.             }
  288.           }
  289.           else
  290.           {
  291.             hStockInfo = hCurrStockInfo;
  292.           }
  293.  
  294.           lpfn = MakeProcInstance((FARPROC) GraphOptionsDlgProc, hThisInstance);
  295.           rc = DialogBoxParam(hThisInstance, "Graph", hWnd, lpfn,
  296.                               MAKELONG(hStockInfo,0));
  297.           FreeProcInstance(lpfn);
  298.  
  299.           if (rc == IDOK)
  300.           {
  301.             hCurrStockInfo = hStockInfo;
  302.           }
  303.           else
  304.           {
  305.             if (wParam == ID_NEW)
  306.               GlobalFree(hStockInfo);
  307.           }
  308.           break;
  309.  
  310.         case ID_OPEN  :
  311.           /*
  312.             Open up a stock file
  313.           */
  314.           lpfn = MakeProcInstance((FARPROC) OpenDlg, hThisInstance);
  315.           DialogBox(hThisInstance, "Open", hWnd, lpfn);
  316.           FreeProcInstance(lpfn);
  317.           break;
  318.  
  319.         case ID_SAVE  :
  320.           if (hCurrStockInfo)
  321.             StockFileSave(hCurrStockInfo);
  322.           break;
  323.  
  324.         case ID_CLOSE :
  325.           if (hWndActive)
  326.             SendMessage(hWndActive, WM_SYSCOMMAND, SC_CLOSE, 0L);
  327.           break;
  328.  
  329.         case ID_PRINT :
  330.           break;
  331.  
  332.         case ID_EXIT  :
  333.           PostQuitMessage(0);
  334.           break;
  335.  
  336.  
  337.         /*
  338.           Add a new tick
  339.         */
  340.         case ID_TICK_ADD :
  341.           /*
  342.             Prompt the user for the new tick info
  343.           */
  344.           lpfn = MakeProcInstance(AddTickDlgProc, hThisInstance);
  345.           rc = DialogBox(hThisInstance, (LPSTR) "AddTick", hWnd, lpfn);
  346.           FreeProcInstance(lpfn);
  347.  
  348.           /*
  349.             See if the user chose OK...
  350.           */
  351.           if (rc == TRUE)
  352.           {
  353.             /*
  354.               If there is new tick information, update the graph for the
  355.               stock. We invalidate the entire MDI child window for this
  356.               stock and call UpdateWindow(). This, in turn, sends a WM_PAINT
  357.               message to the child window.
  358.             */
  359.             if ((lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo)))
  360.             {
  361.               HWND h = lpStockInfo->hWnd;
  362.               GlobalUnlock(hCurrStockInfo);
  363.               InvalidateRect(h, (LPRECT) NULL, TRUE);
  364.               UpdateWindow(h);
  365.             }
  366.           }
  367.           break;
  368.  
  369.         case ID_TICK_CHANGE:
  370.           break;
  371.  
  372.         case ID_GRAPH_PRICE     :
  373.           break;
  374.         case ID_GRAPH_VOLUME    :
  375.           break;
  376.         case ID_GRAPH_ZOOM      :
  377.           break;
  378.         case ID_GRAPH_COLORS    :
  379.           break;
  380.         case ID_GRAPH_GRID_HORZ :
  381.           break;
  382.         case ID_GRAPH_GRID_VERT :
  383.           break;
  384.  
  385.         /*
  386.           MDI functions...
  387.         */
  388.         case ID_WINDOW_TILE     :
  389.           SendMessage(hwndMDIClient, WM_MDITILE, 0, 0L);
  390.           break;
  391.         case ID_WINDOW_CASCADE  :
  392.           SendMessage(hwndMDIClient, WM_MDICASCADE, 0, 0L);
  393.           break;
  394.         case ID_WINDOW_ICONS    :
  395.           SendMessage(hwndMDIClient, WM_MDIICONARRANGE, 0, 0L);
  396.           break;
  397.         case ID_WINDOW_CLOSEALL :
  398.         {
  399.           HWND hwndT;
  400.           ShowWindow(hwndMDIClient, SW_HIDE);
  401.           while ((hwndT = GetWindow(hwndMDIClient, GW_CHILD)) != NULL)
  402.           {
  403.             /* Skip the icon title windows */
  404.             while (hwndT && GetWindow(hwndT, GW_OWNER))
  405.           hwndT = GetWindow(hwndT, GW_HWNDNEXT);
  406.             if (!hwndT)
  407.           break;
  408.             SendMessage(hwndMDIClient, WM_MDIDESTROY, (WORD) hwndT, 0L);
  409.           }
  410.           ShowWindow(hwndMDIClient, SW_SHOW);
  411.           ShowWindow(hWndStatus, SW_SHOW);
  412.           break;
  413.         }
  414.  
  415.         case ID_ABOUT :
  416.           lpfn = MakeProcInstance(AboutDlgProc, hThisInstance);
  417.           rc = DialogBox(hThisInstance, MAKEINTRESOURCE(ID_ABOUT), hWnd, lpfn);
  418.           FreeProcInstance(lpfn);
  419.           break;
  420.  
  421.         default :
  422.           /*
  423.             We might have chosen to change the focus to one of the
  424.             MDI children.
  425.           */
  426.           return DefFrameProc(hWnd, hwndMDIClient, msg, wParam, lParam);
  427.       }
  428.       break;
  429.  
  430.  
  431.     case WM_QUERYENDSESSION:
  432.     case WM_CLOSE:
  433.       SendMessage(hWnd, WM_COMMAND, ID_WINDOW_CLOSEALL, 0L) ;
  434.       if (GetWindow(hwndMDIClient, GW_CHILD) != NULL)
  435.         return FALSE;
  436.       goto call_DFP;
  437.  
  438.  
  439.     case WM_DESTROY :
  440.       PostQuitMessage(0);
  441.       break;
  442.  
  443.  
  444.     case WM_MENUSELECT  :
  445.     {
  446.       WORD idMenu;
  447.       WORD iLen;
  448.  
  449.       /*
  450.         We trigger off of the WM_MENUSELECT message so that we
  451.         can print informative messages about each menu option.
  452.       */
  453.  
  454.       if (lParam & MF_POPUP)
  455.       {
  456.         /*
  457.           The handle of the popup menu is returned in wParam. Search
  458.           the array of sub-menu window handles in order to see which
  459.           submenu we are in.
  460.         */
  461.         for (idMenu = 0;
  462.              ahwndSubMenus[idMenu] && wParam != ahwndSubMenus[idMenu];
  463.              idMenu++)
  464.           ;
  465.         if (!ahwndSubMenus[idMenu])
  466.           break;
  467.         idMenu++;  /* make it a 1-offset value */
  468.       }
  469.       else
  470.         /*
  471.           An actual menu item identifier is returned in wparam...
  472.         */
  473.         idMenu = wParam;
  474.  
  475.       /*
  476.         If lParam was <-1,0>, then the user dismissed the menu, so
  477.         we just want to erase the previous help text from the
  478.         status window. If not, then idMenu contains a valid menu or
  479.         submenu identifier. Load the corresponding string from
  480.         the string table, and print it.
  481.       */
  482.       if (LOWORD(lParam) == -1 && HIWORD(lParam) == 0  ||
  483.          ((iLen = LoadString(hThisInstance, idMenu, (LPSTR) szMsg, sizeof(szMsg)))))
  484.       {
  485.         HDC hDC = GetDC(hWndStatus);
  486.         HBRUSH hBrush, hOldBrush;
  487.  
  488.         /*
  489.           Use a white brush for erasing.
  490.         */
  491.         hBrush = CreateSolidBrush(0x00FFFFFF);
  492.         hOldBrush = SelectObject(hDC, hBrush);
  493.  
  494.         /*
  495.           Erase the previous menu help text, if any. Then display the
  496.           new help text.
  497.         */
  498.         GetClientRect(hWndStatus, (LPRECT) &rClient);
  499.         FillRect(hDC, &rClient, hBrush);
  500.         if (LOWORD(lParam) == -1 && HIWORD(lParam) == 0)
  501.           ;
  502.         else
  503.           TextOut(hDC, 0, 0, szMsg, iLen);
  504.  
  505.         /*
  506.           Restore the old brush.
  507.         */
  508.         SelectObject(hDC, hOldBrush);
  509.         DeleteObject(hBrush);
  510.         ReleaseDC(hWndStatus, hDC);
  511.       }
  512.       break;
  513.     }
  514.  
  515.  
  516.     case WM_SIZE :
  517.       /*
  518.         When the main window is resized, we want to move and resize the
  519.         status window so that it is always lying on the bottom.
  520.       */
  521.       if (hWndStatus)
  522.       {
  523.         GetClientRect(hWnd, (LPRECT) &rClient);
  524.         MoveWindow(hWndStatus, 
  525.                    rClient.left, rClient.bottom - Y_STATUSWINDOW,
  526.                    rClient.right - rClient.left, Y_STATUSWINDOW,
  527.                    TRUE);
  528.       }
  529.       /*
  530.         We need to call DefFrameProc, because it processes the WM_SIZE
  531.         messages too!
  532.       */
  533.       goto call_DFP;
  534.  
  535.  
  536.     default :
  537.       /* Use DefFrameProc() instead of DefWindowProc() since there
  538.        * are things that have to be handled differently because of MDI
  539.        */
  540. call_DFP:
  541.       return DefFrameProc(hWnd, hwndMDIClient, msg, wParam, lParam);
  542.   }
  543.  
  544.  
  545.   return (LONG) TRUE;
  546. }
  547.  
  548.  
  549.  
  550. /****************************************************************************
  551.  *                                        *
  552.  *  FUNCTION   : StatusWndProc ( hwnd, msg, wParam, lParam )            *
  553.  *                                        *
  554.  *  PURPOSE    : Window Proc for the status window.                       *
  555.  *                                        *
  556.  ****************************************************************************/
  557. LONG FAR PASCAL StatusWndProc(hWnd, msg, wParam, lParam)
  558.   HWND hWnd;
  559.   WORD msg;
  560.   WORD wParam;
  561.   LONG lParam;
  562. {
  563.   return DefWindowProc(hWnd, msg, wParam, lParam);
  564. }
  565.  
  566.  
  567. /****************************************************************************
  568.  *                                        *
  569.  *  FUNCTION   : AboutDlgProc ( hwnd, msg, wParam, lParam )            *
  570.  *                                        *
  571.  *  PURPOSE    : Dialog function for the About MultiPad... dialog.        *
  572.  *                                        *
  573.  ****************************************************************************/
  574. BOOL FAR PASCAL AboutDlgProc(hDlg, msg, wParam, lParam)
  575.   HWND hDlg;
  576.   WORD msg;
  577.   WORD wParam;
  578.   LONG lParam;
  579. {
  580.   switch (msg)
  581.   {
  582.     case WM_INITDIALOG:
  583.       /* nothing to initialize */
  584.       break;
  585.  
  586.     case WM_COMMAND:
  587.       switch (wParam)
  588.       {
  589.         case IDOK:
  590.           EndDialog(hDlg, 0);
  591.           break;
  592.  
  593.         default:
  594.           return FALSE;
  595.       }
  596.       break;
  597.  
  598.     default:
  599.       return FALSE;
  600.   }
  601.  
  602.   return TRUE;
  603. }
  604.  
  605.  
  606.  
  607. /****************************************************************************/
  608. /*                                                                          */
  609. /* Function : GraphCreateWindow()                                           */
  610. /*                                                                          */
  611. /* Purpose  : Creates an MDI child window to hold a stock graph in.         */
  612. /*                                                                          */
  613. /* Returns  : The handle of the created MDI child window.                   */
  614. /*                                                                          */
  615. /****************************************************************************/
  616. HWND PASCAL GraphCreateWindow(LPSTR lpName)
  617. {
  618.   HWND hWnd;
  619.   char sz[160];
  620.   MDICREATESTRUCT mcs;
  621.  
  622.   if (!lpName)
  623.   {
  624.     /* If the lpName parameter is NULL, load the "Untitled" string
  625.     *  from STRINGTABLE and set the title field of the MDI CreateStruct.
  626.     */
  627.     strcpy(sz, "(Untitled)");
  628.     mcs.szTitle = (LPSTR) sz;
  629.   }
  630.   else
  631.   {
  632.     /* Title the window with the supplied filename */
  633.     AnsiUpper(lpName);
  634.     mcs.szTitle = lpName;
  635.   }
  636.  
  637.   mcs.szClass = "GraphWindow";
  638.   mcs.hOwner  = hThisInstance;
  639.  
  640.   /* Use the default size for the window */
  641.   mcs.x = mcs.cx = CW_USEDEFAULT;
  642.   mcs.y = mcs.cy = CW_USEDEFAULT;
  643.  
  644.   /* Set the style DWORD of the window to default */
  645.   mcs.style = 0L;
  646.  
  647.   /* tell the MDI Client to create the child */
  648.   hWnd = (WORD) SendMessage(hwndMDIClient,
  649.                 WM_MDICREATE,
  650.                 0,
  651.                 (LONG) (LPMDICREATESTRUCT) &mcs);
  652.   return hWnd;
  653. }
  654.  
  655.  
  656. /****************************************************************************/
  657. /*                                                                          */
  658. /* Function : GraphWndProc()                                                */
  659. /*                                                                          */
  660. /* Purpose  : Window proc for the stock MDI child windows.                  */
  661. /*                                                                          */
  662. /* Returns  :                                                               */
  663. /*                                                                          */
  664. /****************************************************************************/
  665. LONG FAR PASCAL GraphWndProc(hWnd, msg, wParam, lParam)
  666.   HWND hWnd;
  667.   WORD msg;
  668.   WORD wParam;
  669.   LONG lParam;
  670. {
  671.   HANDLE      hStockInfo;
  672.   LPSTOCKINFO lpStockInfo;
  673.  
  674.   switch (msg)
  675.   {
  676.     case WM_CREATE   :
  677.       SetWindowWord(hWnd, 0, NULL);
  678.       break;
  679.  
  680.     case WM_MDIACTIVATE:
  681.       /* If we're activating this child, remember it */
  682.       hWndActive = (wParam) ? hWnd : (HWND) NULL;
  683.       hCurrStockInfo = GetWindowWord(hWnd, 0);
  684.       break;
  685.  
  686.  
  687.     case WM_PAINT     :
  688.     {
  689.       HDC         hDC;
  690.       PAINTSTRUCT ps;
  691.       char        szBuf[80];
  692.  
  693.       hDC = BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
  694.  
  695.       if ((hStockInfo = GetWindowWord(hWnd, 0)) != NULL && 
  696.           (lpStockInfo = (LPSTOCKINFO) GlobalLock(hStockInfo)) != NULL)
  697.       {
  698.         GraphWndPaint(hWnd, hDC, lpStockInfo);
  699.         GlobalUnlock(hStockInfo);
  700.       }
  701.       else
  702.       {
  703.         sprintf(szBuf, "Can't get the handle to the stock info struct");
  704.         TextOut(hDC, 0, 0, (LPSTR) szBuf, strlen(szBuf));
  705.       }
  706.  
  707.       EndPaint(hWnd, &ps);
  708.       break;
  709.     }
  710.  
  711.  
  712.     case WM_QUERYENDSESSION:
  713.     case WM_CLOSE:
  714.       /*
  715.         See if the stock information has been altered during this
  716.         session. If so, prompt the user to see if he really wants
  717.         to close down the stock window.
  718.       */
  719.       if ((hStockInfo = GetWindowWord(hWnd, 0)) != NULL && 
  720.           (lpStockInfo = (LPSTOCKINFO) GlobalLock(hStockInfo)) != NULL)
  721.       {
  722.         if ((lpStockInfo->dwFlags & STATE_DIRTY) && 
  723.              MessageBox(hWnd, "OK to close window?", "Stock",
  724.                               MB_ICONQUESTION | MB_OKCANCEL) == IDCANCEL)
  725.         {
  726.           GlobalUnlock(hStockInfo);
  727.           return FALSE;
  728.         }
  729.         GlobalUnlock(hStockInfo);
  730.       }
  731.       goto call_DCP;
  732.  
  733.  
  734.     case WM_DESTROY:
  735.       /*
  736.         Free all of the stock information which was allocated for a window.
  737.       */
  738.       if ((hStockInfo = GetWindowWord(hWnd, 0)) != NULL && 
  739.           (lpStockInfo = (LPSTOCKINFO) GlobalLock(hStockInfo)) != NULL)
  740.       {
  741.         GlobalFree(lpStockInfo->hTicks);
  742.         GlobalUnlock(hStockInfo);
  743.         GlobalFree(hStockInfo);
  744.       }
  745.       goto call_DCP;
  746.  
  747.     default:
  748.       /*
  749.         Again, since the MDI default behaviour is a little different,
  750.         call DefMDIChildProc instead of DefWindowProc()
  751.       */
  752. call_DCP:
  753.       return DefMDIChildProc(hWnd, msg, wParam, lParam);
  754.   }
  755.  
  756.   return (LONG) FALSE;
  757. }
  758.  
  759.  
  760.